import axios, { AxiosResponse } from "axios";


const API = axios.create({
	baseURL: import.meta.env.VITE_APP_BASE_API || '',
	timeout: 10000
  })

API.interceptors.request.use(
	(config) => {
	  config.headers.Authorization = localStorage.getItem('token');
	  return config;
	},
	(error) => {
	  return Promise.reject(error);
	}
  );

API.interceptors.response.use((result: AxiosResponse)=>{
	console.log(result);
	if(result.status ==200){
		if(result.data.code == 200){
			return result.data
		}else if(result.data.code == 401){
			return window.location.href = "/login";
		}
	}


	// if (result.request.responseType ===  'blob' || result.request.responseType ===  'arraybuffer') {
	// 	return result.data
	//   }

},
(err) => {
    return Promise.reject(err);
  })
export default API